package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.BedMapper;
import cn.tedu.carehome.foundation.mapper.CheckInfoMapper;
import cn.tedu.carehome.foundation.mapper.RoomMapper;
import cn.tedu.carehome.foundation.pojo.dto.RoomDTO;
import cn.tedu.carehome.foundation.pojo.entity.Bed;
import cn.tedu.carehome.foundation.pojo.entity.Room;
import cn.tedu.carehome.foundation.pojo.vo.BedAdminStandardVO;
import cn.tedu.carehome.foundation.pojo.vo.CheckInfoStandardVo;
import cn.tedu.carehome.foundation.service.IRoomService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class IRoomServiceImpl implements IRoomService {

    @Autowired
    CheckInfoMapper checkInfoMapper;
    @Autowired
    BedMapper bedMapper;
    @Autowired
    RoomMapper roomMapper;

    @Override
    public void addnew(RoomDTO roomDTO) {
        log.debug("开始处理床位添加业务");
        CheckInfoStandardVo checkInfoStandardVo = checkInfoMapper.getStandarByid(roomDTO.getCheckId());
//        if(checkInfoStandardVo==null)
//        {
//            log.debug("入住失败,入住登记信息不存在;");
//            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"入住失败,入住登记信息不存在;");
//        }
//        BedAdminStandardVO bedAdminStandardVO = bedMapper.getStandardByid(roomDTO.getBedId());
//        if(bedAdminStandardVO==null)
//        {
//            log.debug("入住失败,床位信息不存在;");
//            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"入住失败,床位信息不存在;");
//        }
//        if(roomMapper.countByCheckId(roomDTO.getCheckId())>0)
//        {
//            log.debug("入住失败,该床位已有人入住");
//            throw new ServiceException(ServiceCode.ERR_CONFLICT,"入住失败,该床位已有人入住");
//        }
        Room room= new Room();
        BeanUtils.copyProperties(roomDTO,room);
        roomMapper.insert(room);
        Bed bed = new Bed();
        bed.setId(roomDTO.getBedId());
        bed.setEnable(1);
        int l = bedMapper.update(bed);
        if(l!=1)
        {
            log.debug("入住失败,服务器正忙,请稍后再试！");
             throw new ServiceException(ServiceCode.ERR_INSERT,"入住失败,服务器正忙,请稍后再试！");
        }
        log.debug("入住成功！");
    }
}
